<?php
namespace app\first\model;

use app\admin\model\Pic;
use think\db\Where;
use think\Model;
/**
 * Class Push
 * @package app\push\validate
 */
class Active extends Model{
    protected $autoWriteTimestamp = false;

    /**
     * @param array $where
     * @param string $order
     * @param int $limit
     * @param string $filed
     * @param bool $out
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($where=[],$order='create_time desc',$limit=10,$filed='*',$out=false){
        $list = $this
            ->field($filed,$out)
            ->with(['user'=>function($query){
                $query->field('id,account,nickname,sex,avatar,is_auth,is_vip,vip_type');
            }])
            ->where($where)
            ->order($order)
            ->paginate($limit);
//        foreach ($list as $k => $v){
//        }
        return $list;
    }

    /**
     * @param int $id
     * @param string $filed
     * @return array|\PDOStatement|string|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getOneById($id=0){
        $res = $this->find($id);
        return $res;
    }

    public function user(){
        return $this->hasOne('member','id','mid');
    }

    /**
     * @param $cid
     * @param int $limit
     * @return int
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function likes($limit=4){
        $_limit = $limit * 5;
        $sql = "SELECT t1.id,t1.title,t1.image,t1.create_time FROM `{$this->getTable()}` AS t1 
          JOIN (SELECT ROUND(RAND() * ((SELECT MAX(id) FROM `{$this->getTable()}`)-(SELECT MIN(id) 
          FROM `{$this->getTable()}`))+(SELECT MIN(id) FROM `{$this->getTable()}`)) AS id from `{$this->getTable()}` limit {$_limit}) AS 
          t2 on t1.id=t2.id where t1.status = 2 ORDER BY t1.id LIMIT {$limit}";
        $list = $this->query($sql);
        return $list;
    }

    /**
     * @param string $order
     * @param int $limit
     * @param string $filed
     * @param bool $out
     * @return array|\PDOStatement|string|\think\Collection|\think\model\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function lastList($order='create_time desc',$limit=10,$filed='*',$out=false){
        $where = new Where;
        $where['status']=1;
        $list = $this
            ->field($filed,$out)
            ->with(['user'=>function($query){
                $query->field('id,account,nickname,sex,avatar,is_auth,is_vip,vip_type');
            }])
            ->where($where)
            ->order($order)
            ->limit($limit)
            ->select();
        return $list;
    }

    /**
     * @param $q
     * @param $where
     * @param $limit
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function search($q,$where,$limit){
        $list = $this
            ->field('id,content,create_time')
            ->where($where)
            ->whereLike('content',"%$q%")
            ->paginate($limit);
        return $list;
    }
    /**
     * @param $value
     * @return array|bool|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFounÍdException
     * @throws \think\exception\DbException
     */
    public function getImageAttr($value){
        if(empty($value)){
            return '';
        }
        $pic = new Pic();
        return $pic->getUrlsById(explode(',',$value));
    }
}